package fr.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import fr.business.ManageApplicationsBusinessService;
import fr.form.Application;
import fr.form.synthese.SyntheseEcranApplications;

@Controller
@RequestMapping("/moa")
public class ManageMoaApplicationsController {

	@Autowired
	private ManageApplicationsBusinessService bs;

	@RequestMapping("/showApps")
	public String listApplications(Map<String, Object> map, @ModelAttribute("ApplicationMOAForm")
	SyntheseEcranApplications ecran) {
		List<Application> apps = bs.getAllApplications();
		ecran.setMapStatutsFromApp(apps);
		map.put("listApplication", apps);
		map.put("listeStatut", bs.getMapStatutsApplication());
		return "moa/showApps";
	}
	
	@ResponseBody
	@RequestMapping(value="/modifier",method=RequestMethod.POST)
	public String modifier(@RequestParam(value="id") Integer id,
			@RequestParam(value="statut") Integer statut){

		if(bs.updateApplicationMoa(id,statut)){
			return "success";
		}
		return "error";
	}

	@RequestMapping(value="/modifierTout",method=RequestMethod.POST)
	public String validerModificationMoa(Map<String, Object> map,@ModelAttribute("ApplicationMOAForm")
	SyntheseEcranApplications ecran, BindingResult result) {
		bs.updateStatutsApplicationsMoa(ecran);
		return "redirect:/moa/showApps";
	}



}

